Home:ALL Converter>Produce lowercase character instead of uppercase in Java

Produce lowercase character instead of uppercase in Java

Ask Time:2021-10-19T16:24:56         Author:iman farhana

Json Formatter

The output is fine but the output is in Uppercase letter, I want the output to be lowercase character, what should I do?

import java.util.Scanner;

public class Q2 {
      
        public static void main(String[] args) {
            int a[]=new int[26];
            Scanner sc =  new Scanner (System.in);
            String str=sc.nextLine();
            for(int i = 0; i<str.length();i++) {
                if(str.charAt(i)>=65 && str.charAt(i)<=90) {
                    a[str.charAt(i)-65]++;
                }
                else if(str.charAt(i)>=97 && str.charAt(i)<=122) {
                    a[str.charAt(i)-97]++;
                }
            }
            for(int i=0;i<26;i++) {
                if(a[i]>0) {
                    System.out.print(" "+(char )(i+65)+ "(" + a[i]+")");
                }
                
            }
                    
        }       

}

Author:iman farhana,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/69627418/produce-lowercase-character-instead-of-uppercase-in-java
Joachim Sauer :

'A' is 65. 'a' is 97. Use 97 instead of 65 in your final loop.\nAlternatively use 'a' directly instead of the number. This makes it more obvious what you're doing in that code.",
2021-10-19T08:28:53
Nowhere Man :

It may be more straightforward to convert the entire string to lower case to avoid checking for upper case letters.\nAlso, as suggested earlier using character literals as a instead of 97 would make the code more readable.\nScanner sc = new Scanner(System.in);\nint a[] = new int[26];\n\nString str = sc.nextLine().toLowerCase();\n\nfor (char c : str.toCharArray()) {\n if (c >= 'a' && c <= 'z') {\n a[c - 'a']++;\n }\n}\nfor (char c = 'a'; c <= 'z'; c++) {\n int i = c - 'a';\n if (a[i] > 0) {\n System.out.print(" "+ c + "(" + a[i] + ")");\n }\n}\n",
2021-10-20T12:36:08
yy